
import { Component, OnInit, ViewChild } from '@angular/core';
import 'rxjs/add/operator/map';
import { CommService } from '../../comm/comm.service';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
declare const $: any;
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDropDownBoxComponent } from 'devextreme-angular';
import { GlobalDataManager } from 'app/ptpmc/comm/global-data-manager';
import * as Enumerable from 'linq';
@Component({
    selector: 'app-moee',
    templateUrl: './moee.component.html',
    styleUrls: ['./moee.component.scss']
})
export class MoeeComponent implements OnInit {
    title = '月度设备有效利用率';
    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public subAreaSelected: any;
    public shiftItems: any[];
    public shiftSelected: any;
    public equipemntItems: TVResource[] = [];
    public equipmentSelected: TVResource[];
    public queryStationCode;

    public startHour = false;
    public endHour = false;
    public eat = false;
    public change = false;
    public moeeList = [];
    public moee = 0;
    public jph = 0;

    @ViewChild('dddb') dddb: DxDropDownBoxComponent;
    @ViewChild('ddda') ddda: DxDropDownBoxComponent;

    constructor(private commServices: CommService) {
    }

    ngOnInit() {
        this.commServices.initDatePicker($('#DateTimeRange'), 'YYYY-MM-DD');
        this.shiftItems = this.commServices.shifts;
        this.shiftSelected = this.shiftItems[0];
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAreaSelected = [];
        this.equipemntItems = [];
        this.subAreaSelected.forEach(e => {
            const arr = this.commServices.getTVResourceBySubArea(e.subArea);
            arr.forEach(a => { this.equipemntItems.push(a); });
        });

        this.equipmentSelected = [];

        this.onSubAreaSelectedChange();

        

    }

    clearEquipmentSelected() {
        this.equipmentSelected = [];
    }
    equipmentSelectedOk() {
        this.dddb.instance.close();
    }
    clearSubAreaSelected() {
        this.subAreaSelected = [];
    }
    subAreaSelectedOk() {
        this.ddda.instance.close();
    }

    onSubAreaSelectedChange(): void {
        this.equipemntItems = [];
        const arr = this.commServices.getTVResourceBySubArea(this.subAreaSelected.subArea);
        arr.forEach(a => { this.equipemntItems.push(a); });
        const queryModel: QueryModel = new QueryModel();
        queryModel.querySubAreaIds = [this.subAreaSelected.subArea];
        if (this.subAreaSelected !== undefined) {
            this.commServices
                .getList(queryModel, '/moee/station')
                .subscribe(model => {
                    if (model.resultCode === 200) {
                        this.queryStationCode = model.data.queryStation;
                        this.equipmentSelected = this.equipemntItems.filter(e => e.stationCode === this.queryStationCode);
                    } else {
                        alert('查询错误：' + model.resultMessage);
                    }
                });
        }
    }

    getReportData(): void {
        if (this.queryStationCode === undefined || this.queryStationCode === null) {
            alert('请问配置该区域的工位再查询！');
            return;
        }
        const queryModel: QueryModel = new QueryModel();
        queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = [this.subAreaSelected.subArea];
        queryModel.queryResourceIds = [this.queryStationCode];
        queryModel.shift = this.shiftSelected.shiftId;
        queryModel.b1 = this.startHour;
        queryModel.b2 = this.endHour;
        queryModel.b3 = this.eat;
        queryModel.b4 = this.change;
        this.commServices
            .getList(queryModel, '/prodCount/moee')
            .subscribe(model => {
                if (model.resultCode === 200) {
                    debugger
                    this.moeeList = [];
                    model.data.oee = Enumerable.from(model.data.oee).orderBy((e:any)=>e.prodDateVal0).thenBy((e:any)=>e.shiftVal0).toArray();
                    model.data.oee.forEach(e => this.moeeList.push(e));
                    this.jph = model.data.jph
                   
                    this.moeeList.push({
                        prodDateVal0: '生产线:',
                        shiftVal0: GlobalDataManager.getProjectSelected().projectName,
                        oee1: '区域:',
                        oee2: this.subAreaSelected.subArea,
                        oee4: 'JPH:',
                        oee5: model.data.jph,
                        oee7: '平均OEE:',
                        oee8: model.data.all,
                    });
                    this.moee = model.data.all;
                } else {
                    alert('查询错误：' + model.resultMessage);
                }
            });
    }

    getTime(id: any): String {
        return $('#' + id).val();
    }

    ex(){
        this.commServices.exporttable2Excel('tb');
    }

    onExporting(e) {
        e.fileName = e.fileName + new Date().toISOString().replace(/[\-\:\.]/g, '').split('T')[0];
    }
}